﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace template_dll
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        public DataTable dt;
        public int pharmcode = 0;
        public string pharmname = "";


        private void HideShowRows(TextBox tb, DataGridView dgv)
        {
            CurrencyManager cm = (CurrencyManager)dgv.BindingContext[dgv.DataSource];
            cm.SuspendBinding();
            foreach (DataGridViewRow r in dgv.Rows)
            {
                r.Visible = true;
                if (!Regex.Match(Convert.ToString(dgv[dgv.SortedColumn.Name, r.Index].Value), tb.Text, RegexOptions.IgnoreCase).Success)
                    r.Visible = false;
            }
            cm.ResumeBinding();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = dt.DefaultView;
            dataGridView1.Columns[1].Width *= 3;
            dataGridView1.Sort(dataGridView1.Columns[1], ListSortDirection.Ascending);
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            pharmcode = Convert.ToInt32(dataGridView1[0, e.RowIndex].Value);
            pharmname = dataGridView1[1, e.RowIndex].Value.ToString();
            this.DialogResult = DialogResult.OK;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            //- поиск по полю
            HideShowRows(textBox1, dataGridView1);
        }
    }
}
